import grace from "../../../utils/grace.js";
var app = getApp();

grace.page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad() {
    let school_id = wx.getStorageSync('school_id')
    let that = this;
    if (wx.getStorageSync('userInfo')) {
      that.$data = {
        userInfo: wx.getStorageSync('userInfo'),
        hasUserInfo: true
      }
    } else if (that.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.$data = {
          userInfo: res.userInfo,
          hasUserInfo: true
        }
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          that.$data = {
            userInfo: res.userInfo,
            hasUserInfo: true
          }
        }
    })
    }
  },
  getUserInfo(e) {
    wx.setStorageSync('userInfo', e.detail.userInfo);
    this.$data = {
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    }
    this.login();
  },
  /* 登录 */
  login () {
    let that = this;
    let token = wx.getStorageSync('token');
    let school_id = wx.getStorageSync('school_id')
    if (token) {// 有token调用api校验token
      wx.showLoading({
        title: '加载中...',
      })
      let url = app.globalData.api + '/api/v1.0/wechatUserCheckTokenAPI/' + school_id;
      that.$http.get(url, { token: token }).then((res) => {
        wx.hideLoading();
        if (res.data.code != 0) {
          wx.removeStorageSync('token');
          that.login();
        } else {
          wx.setStorageSync('token', token);
          wx.setStorageSync('partner_id', res.data.data.partner_id);
          that.$goBack({});
        }
      }).catch(err => {
        console.log(err)
      });
    }else{
      wx.login({// 没有token时登录
        success(res) {
          let school_id = wx.getStorageSync('school_id')
          let url = app.globalData.api + '/api/v1.0/weChatUserLoginAPI/' + school_id + '/' + res.code;
          that.$http.get(url).then((ret) => {
            if (ret.data.code == 0) {
              wx.setStorageSync('token', ret.data.data.token);
              wx.setStorageSync('partner_id', ret.data.data.partner_id);
              that.$goBack({});
            } else if (ret.data.code == 10000) {
              that.registerUser();
            } else {
              // 登录错误
              wx.showModal({
                title: '提示',
                content: '无法登录，请重试',
                showCancel: false
              })
            }
          }).catch(err => {
            console.log(err)
          });
        }
      })
    }
  },
  /* 注册 */
  registerUser () {
    let that = this;
    wx.login({
      success (res) {
        let code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
        wx.getUserInfo({
          success (ret) {
            let iv = ret.iv;
            let encryptedData = ret.encryptedData;
            let school_id = wx.getStorageSync('school_id')
            let url = app.globalData.api + '/api/v1.0/weChatUserRegisterComplexAPI/' + school_id;
            that.$http.post(url, { code: code, encryptedData: encryptedData, iv: iv }).then((res) => {
              if (res.data.code == 0) {
                wx.hideLoading();
                that.login();
              }
            }).catch(err => {
              console.log(err)
            });
          },
          fail (err) {
            console.log('error')
          }
        })
      }
    })
  },
  cancel(){
    wx.switchTab({
      url: '/pages/home/index/index'
    })
  }
})